package com.vnt;



import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.core.KuixConstants;
import org.kalmeo.kuix.core.model.DataProvider;
import org.kalmeo.kuix.util.Metrics;
import org.kalmeo.kuix.widget.ListItem;
import org.kalmeo.kuix.widget.Screen;
import org.kalmeo.kuix.widget.Widget;
import org.kalmeo.kuix.widget.Screen.ScreenMenu;
import org.kalmeo.util.frame.Frame;

import com.ilove.core.ILOVEContants;
import com.ilove.frame.AboutFrame;
import com.ilove.frame.NullFrame;


public class SubIndexFrame implements Frame {

	// static instance of MainFrame
	public static SubIndexFrame instance = new SubIndexFrame();
	public int id;// 0 text data from vas, 1 ringtone, 2 game ,3 wallpaper
	public String code;
	public String name;
	public String code_name;
	public static boolean getListPage = false;
	private int currPage = 1;
//	private int totalPage = 1;
	public String[] vecContent = null;
	boolean isShort = false;

	// MainFrame's Screen
	private Screen screen;

	// The Pad that hold all shopLists
	private final Pad pad = new Pad();

	/**
	 * Show the screen
	 */
	public void showScreen() {
		if (screen != null) {
			screen.setCurrent();
		}
	}

	public void onAdded() {
		try {
			System.out.println("Sub Index frame on add");
			String[] subIndex = pad.getGroupContent(code,id,name);
			if (subIndex != null && subIndex.length > 0) {
				System.out.println("subIndex is not null");
				pad.getSubIndex(subIndex, name);
				if(getListPage==true){
					screen = Kuix.loadScreen(ILOVEContants.PAGE_SCREEN, pad);
					isShort = false;
					screen.setCurrent();
				int screenWidth = Kuix.getCanvas().getWidth();
				ScreenMenu rightMenu = (ScreenMenu)screen.getSecondMenu();
				Metrics metrics = rightMenu.getPreferredSize(screenWidth);
				int containerWidth = screenWidth - (2*metrics.width)-10;
				Widget container = screen.getWidget("CTM_PAGE");
				container.setAttribute("style", "layout:flowlayout(true, fill);align:center;min-size:"+containerWidth + " 16");
				}
				else{
					screen = Kuix.loadScreen(ILOVEContants.SUBINDEX_SCREEN, pad);
					isShort = false;
					screen.setCurrent();
				}
			} else {
				System.out.println("sub Index is null");
				ContentFrame.instance.id = id;
				ContentFrame.instance.contentId = code;
				ContentFrame.instance.contentName = name;
				Kuix.getFrameHandler().pushFrame(ContentFrame.instance);
			}
			
			
		} catch (Exception e) {
			Kuix.alert(e);
			e.printStackTrace();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.kalmeo.util.frame.Frame#onMessage(java.lang.Object,
	 *      java.lang.Object[])
	 */
	
	public boolean onMessage(Object identifier, Object[] arguments) {
		if ("askQuit".equals(identifier)) {
			// show askQuit box wich call onMessage method with "quit" name and
			// null arguments
			Kuix.alert(Kuix.getMessage("ASK_QUIT"),
					KuixConstants.ALERT_QUESTION | KuixConstants.ALERT_YES
							| KuixConstants.ALERT_NO, "!exit", null);
			return false;
		} else if ("next".equals(identifier)) {
			Widget focusedWidget = screen.getFocusManager().getFocusedWidget();
			System.out.println("Sub Index Show");
			if (focusedWidget instanceof ListItem) {
				DataProvider dataprovider = ((ListItem) focusedWidget).getDataProvider();
				if (dataprovider != null) {
					SubIndexList subIndexList = (SubIndexList) dataprovider;
					try {
						if(id==0){
						System.out.println("id="+id);
						SubIndexFrame.instance.code = subIndexList.code;
						SubIndexFrame.instance.name = subIndexList.name;
						SubIndexFrame.instance.onAdded();
						}
						else if(id==5){
						ContentFrame.instance.id = id;
						ContentFrame.instance.contentId = subIndexList.code;
						ContentFrame.instance.contentName = subIndexList.name;	
						Kuix.getFrameHandler().pushFrame(ContentFrame.instance);
						}
						else if(id==4 && (code_name=="SOI" || code_name=="CAU")){
							Kuix.getFrameHandler().pushFrame(NullFrame.instance);
						}
						else{
						ContentListFrame.instance.id = id;
						ContentListFrame.instance.code = subIndexList.code;
						if(id==4){
							ContentListFrame.instance.name = "XOSO";
						}
						else if(name=="Truyen" || name == "MEO VAT")
							ContentListFrame.instance.name = name;
						else
							ContentListFrame.instance.name = subIndexList.name;
						System.out.println("ContentListFrame.instance.code="+subIndexList.code);
						System.out.println("ContentListFrame.instance.name="+subIndexList.name);
						Kuix.getFrameHandler().pushFrame(ContentListFrame.instance);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		} else if ("back".equals(identifier)) {
			Kuix.getFrameHandler().removeFrame(instance);
			return false;
		}  else if ("arrLeft".equals(identifier) && getListPage==true) {
			if (currPage > 1) currPage--;
			System.out.println("currPage"+currPage);
			Pad.page = currPage;
			screen.cleanUp();
			screen = null;
			ILOVEContants.INDEX_LIST = null;
			SubIndexFrame.instance.onAdded();
			} else if ("arrRight".equals(identifier) && getListPage==true) {
//			if (currPage < totalPage) 
			currPage++;
			System.out.println("currPage"+currPage);
			ILOVEContants.INDEX_LIST = null;
			Pad.page = currPage;
			SubIndexFrame.instance.code = Pad.tmp_code;
			SubIndexFrame.instance.onAdded();
		}
		return true;
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see org.kalmeo.util.frame.Frame#onAdded()
	 */

	public void onRemoved() {
		// show MainFrame screen
		try {
			IndexFrame.instance.showScreen();
			screen.cleanUp();
			screen = null;
		} catch (Exception e) {
		}

	}
}